package com.bjpowernode.sparsearray;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;

/**
 * @ClassName Demo2
 * @Description TODO 读取文件中存储的对象，并进行复盘 —— 读取出来的是稀疏数组，需要将之转换成正常的二维数组
 * @date 2021/9/14 17:17
 * @Author ZY
 */
public class Demo2 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("DataStructure/myfile.data"));
        int[][] spareArray = (int[][])in.readObject(); // 稀疏数组
        int[][] target = new int[spareArray[0][0]][spareArray[0][1]]; // 棋盘
        for (int i = 0; i < spareArray.length; i++) {
            for (int j = 0; j < spareArray[i].length; j++) {
                System.out.print(spareArray[i][j] + "\t");
            }
            if (i!=0){
                target[spareArray[i][0]][spareArray[i][1]] = spareArray[i][2];
            }
            System.out.println();
        }

        System.out.println("=============================");
        // 还原棋盘
        for (int[] ints : target) {
            for (int anInt : ints) {
                System.out.print(anInt + "\t");
            }
            System.out.println();
        }

    }
}
